import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index";

import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false });

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login.vue"),
    meta: {
      title: "登录",
      icon: "dashboard",
      noCache: true,
      affix: false,
      isAuth: false,
    },
  },
  {
    path: "/home",
    name: "home",
    redirect: "/home/welcome",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/index.vue"),
    meta: {
      title: "",
      icon: "dashboard",
      noCache: true,
      affix: false,
      isAuth: false,
    },
    children: [
      {
        path: "welcome",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/Start.vue"),
        name: "控制台",
        meta: {
          title: "控制台",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "operatorlist",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/profile/operatorlist.vue"
          ),
        name: "管理员",
        meta: {
          title: "管理员",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "rolegrouplist",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/profile/rolegrouplist.vue"
          ),
        name: "权限组",
        meta: {
          title: "权限组",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "profile",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/profile/index.vue"
          ),
        name: "个人中心",
        meta: {
          title: "个人中心",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: false,
        },
      },
      // 开发中
      {
        path: "salesforcast",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/results/salesforcast.vue"
          ),
        name: "汽车销量预测",
        meta: {
          title: "汽车销量预测",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "forcastresult",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/results/forcastresult.vue"
          ),
        name: "销量预测结果",
        meta: {
          title: "销量预测结果",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "salesforcastonline",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/online/salesforcastonline.vue"
          ),
        name: "在线销量预测",
        meta: {
          title: "在线销量预测",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "priceforcastonline",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/online/priceforcastonline.vue"
          ),
        name: "在线销量预测",
        meta: {
          title: "在线销量预测",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "statisticalanalysis",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/statistics/statisticalanalysis.vue"
          ),
        name: "统计分析",
        meta: {
          title: "统计分析",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "carinfomation",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/statistics/carinfomation.vue"
          ),
        name: "汽车配置信息",
        meta: {
          title: "汽车配置信息",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      },
      {
        path: "salesrank",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/system/rank/salesrank.vue"
          ),
        name: "销量排行榜",
        meta: {
          title: "销量排行榜",
          icon: "dashboard",
          noCache: true,
          affix: false,
          isAuth: true,
        },
      }
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  routes,
});

// 冗余导航错误捕获
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => {
    NProgress.done();
  });
};

//前置路由守卫
//只要做路由跳转，beforeEach都可以获得当前的路由地址和跳转的目标地址,  调用next()函数可以放行
router.beforeEach((to, from, next) => {
  NProgress.start();
  // console.log(store.state)
  // 可访问路由列表  
  if (to.meta.isAuth || to.meta.isAuth == undefined) {
    // 判断是否已经登录
    let token = store.state.permission.token;
    if (token == undefined || token == null) {
      router.push("/login");
    }
    // 判断是否拥有权限访问
    let authList = store.state.permission.authRoutes;
    if (authList.includes(to.fullPath)) {
      // console.log(to.fullPath);
      next();
    } else {
      router.app.$message({
        message: "您访问的地址不存在或未授权，请重新登录",
        type: "error",
      });
      router.push("/login");
    }
  } else {
    next();
    NProgress.done();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
